//保存当前页面编辑状态下是否有内容需要保存
var unloadPageAlert = false ;
var closeDialog = false ;
var currentArtDialog ;
var curCurPos = -1 ;
var tipCloseWindow = false ;
var tipCloseWindowMessage = "" ;
var defaultLoadHtml = '<div class="conTop" style="padding:0 20px;"><img src="../../../style/images/loading.png" width="35" height="35" alt=""><h2>页面正在载入，请稍候......</h2></div>';

(function (d) {
    d['okValue'] = '确定';
    d['cancelValue'] = '取消';
    d['title'] = '消息';
    // [more..]
})(art.dialog.defaults);

function tipArtPage(title , url , artwidth , lock){
    return tipArtPageWithLock(title , url, artwidth , true);
}

function tipArtPageWithLock(title , url , artwidth , lock){
    var list = art.dialog.list;
    for (var i in list) {
        list[i].close();
    };
    currentArtDialog = art.dialog({
        lock: lock,
        id:'curDialog',
        fixed: true,
        title: title,
        width:260,
        padding:'0px 0px',
        drag:true,
        content:defaultLoadHtml,
        initialize: function () {
            var currentDialog = this;
            $.ajax({
                url:url,
                cache:false,
                success: function(data){

                    $('.d-content').empty().html(data);

                    $('.middleArt').css("width",artwidth);

                    currentDialog._reset();
                    $("input[type='checkbox']").css("border","solid 0px #fff");
                    $("input[type='radio']").css("border","solid 0px #fff");

                },
                error:  function(xhr, type, s){
                    if(xhr.getResponseHeader("emsg")){
                        art.alert(xhr.getResponseHeader("emsg"));
                    }
                }
            }).done(function(){

            });
        }
    });
}

$(document).ready(function(){
    $('body').on('click', '[data-toggle="ajax"]', function ( e ) {
        var befor = $(this).attr("data-befor");
        if(befor != null && befor != ""){
            var ret = eval(befor);
            if(!ret){
                return false;
            }
        }
        closeDialog = true ;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var artwidth = $(this).attr("data-width") ? $(this).attr("data-width") : 750 ;
        var lock = $(this).attr('data-lock') ;

        if(lock == "false"){
            tipArtPageWithLock(title , url , artwidth , false);
        }else{
            tipArtPage(title , url , artwidth);
        }
        return false;
    });

    //提交表单并弹出窗口
    $('body').on('click', '[data-toggle="ajax-dialog-form"]', function ( e ) {
        closeDialog = true ;
        var url = $(this).attr("href");
        var title = $(this).attr("title");
        var form = "#"+$(this).attr("form");
        var artwidth = $(this).attr("data-width") ? $(this).attr("data-width") : 750 ;
        currentArtDialog = art.dialog({
            lock: true,
            id:'curDialog',
            fixed: true,
            title: title,
            width:260,
            padding:'0px 0px',
            drag:true,
            content:defaultLoadHtml,
            initialize: function () {
                var currentDialog = this;

                if($(form).valid()){
                    $(form).ajaxSubmit({
                        url:url,
                        success: function(data){
                            $('.d-content').empty().html(data);
                            $('.middleArt').css("width",artwidth);
                            currentArtDialog._reset();
                        },
                        error:function(xhr, type, s){
                            if(xhr.getResponseHeader("emsg")){
                                art.alert(xhr.getResponseHeader("emsg"));
                            }
                        }
                    });
                }
            }
        });
        return false;
    });
    /**
     *表单验证
     */
    $('body').on('submit.form.data-api', '[data-toggle="ajax-form"]', function ( e ) {
        closeDialog = true ;
        var close = $(e.target).attr("data-close");
        if(close){
            closeDialog = false ;
        }
        var formValue = $(e.target) ;
        notification("请稍候，正在加载······",true);	//开始
        if($(this).valid()){
            $(this).ajaxSubmit({
                url:formValue.attr("action"),
                success: function(data){
                    if(formValue.attr("data-callback")){
                        eval(formValue.attr("data-callback")+'($(formValue.attr("data-target")) , data)');
                    }else{
                        if(formValue.attr("data-target")){
                            $(formValue.attr("data-target")).empty().html(data);
                        }
                    }
                    notification("",false);	//结束
                    if(closeDialog && currentArtDialog!=null){
                        currentArtDialog.close();
                    }
                },
                error:function(xhr, type, s){
                    notification("",false);	//结束
                }
            });
        }else{
            notification("",false);	//结束
        }
        return false;
    });

    $('body').on('submit.form.data-api', '[data-toggle="ajax-noclose-form"]', function ( e ) {
        closeDialog = true ;
        var close = $(e.target).attr("data-close");
        if(close){
            closeDialog = false ;
        }
        var formValue = $(e.target) ;
        notification("请稍候，正在加载······",true);	//开始
        if($(this).valid()){
            $(this).ajaxSubmit({
                url:formValue.attr("action"),
                success: function(data){

                    if(formValue.attr("data-callback")){
                        eval(formValue.attr("data-callback")+'($(formValue.attr("data-target")) , data)');
                    }else{
                        if(formValue.attr("data-target")){
                            $(formValue.attr("data-target")).empty().html(data);
                        }
                    }
                    notification("",false);	//结束
                }
            });
        }else{
            notification("",false);	//结束
        }
        return false;
    });

    $('body').on('submit.form.data-api', 'form', function ( e ) {
        if($(this).valid()){
            return true;
        }else{
            return false;
        }
    });

    $('body').on('click.a.data-api', '[data-toggle="load"]', function ( e ) {
        closeDialog = true ;
        var href = 	$(this).attr('href') ;
        var target = 	$(this).attr('data-target') ? $(this).attr('data-target') : ".d-content" ;
        if($(target)){
            $(target).empty().html(defaultLoadHtml);
        }
        loadURL(href , target,function(){
            $('.middleArt').css("width","100%");
        });
        return false;
    });

    $('body').on('click.a.data-api', '[data-toggle="delete"]', function ( e ) {
        var href = 	$(this).attr('href') ;
        art.confirm($(this).attr('data-title') , function(){
            location.href =  href;
        } , function(){});
        return false ;
    });
});

function loadURL(url , panel , callback ){
    loadURLWithTip(url  , panel , callback , true) ;
}

function loadURLWithTip(url , panel , callback  , tip){
    if(tip == true){
        notification("请稍候，正在加载······",true);	//开始
    }
    $.ajax({
        url:url,
        cache:false,
        success: function(data){
            if(panel){
                $(panel).empty().html(data);
            }
            if(callback){
                callback(data);
            }
        },
        error:  function(xhr, type, s){
            if(xhr.getResponseHeader("emsg")){
                art.alert(xhr.getResponseHeader("emsg"));
            }
        }
    }).done(function(){
        if(tip == true){
            notification("",false);	//开始
        }
    });
}

function closeArtDialog(){
    if(currentArtDialog){
        currentArtDialog.close();
    }
}

function notification(_message , type,isshadow){
    if(type){
        $('.notification').remove();
        $('.shadow_notification').remove();
        var shadow = "";
        if(isshadow==undefined||isshadow){
            shadow = "<div class=\"d-mask shadow_notification\" style=\"z-index: 100000; position: fixed; left: 0px; top: 0px; width: 100%; height: 100%; overflow: hidden; display: block;\"></div>";
        }

        var p = $("<p/>").addClass('notification');
        $('body').append(p);
        $('body').append(shadow);
        var wid = parseInt($(document).width()/2-$(p)[0].offsetWidth/2);
        $(p).css('left',wid.toString()+'px');
        $(p).css('top',$(document).height()*1/3+'px');
        $(p);
    }else{
        if($(".notification").length>0){
            $('.notification').fadeOut();
        }
        if($(".shadow_notification").length>0){
            $('.shadow_notification').fadeOut();
        }

    }
}